package com.yanqu.road.dao.impl.nenmonopolymarket;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketReportData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewMonopolyMarketReportDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketReportData> {

    public static String getNewMonopolyMarketReportDataSql = "select * from `t_u_new_monopoly_market_report_data` where `userId`=? and `weekId`=?";
    public Map<Integer, List<NewMonopolyMarketReportData>> getNewMonopolyMarketReportData(long userId, int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<NewMonopolyMarketReportData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getNewMonopolyMarketReportDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketReportData data = parse(rs);
                if(!map.containsKey(data.getType())){
                    map.put(data.getType(), new ArrayList<>());
                }
                map.get(data.getType()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateNewMonopolyMarketReportDataSql = "update `t_u_new_monopoly_market_report_data` set resGroupId=?,type=?,posIndex=?,robId=?,isWin=?,time=?,robInfo=?,actionType=? where userId=? and weekId=?";
    @Override
    protected boolean updateData(NewMonopolyMarketReportData newMonopolyMarketReportData) {
        newMonopolyMarketReportData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getResGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getPosIndex()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketReportData.getRobId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketReportData.getIsWin()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketReportData.getRobInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getActionType()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketReportData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getWeekId()));
        if (getDb().executeNoneQuery(updateNewMonopolyMarketReportDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMarketReportDataSql = "insert into `t_u_new_monopoly_market_report_data`(userId,weekId,resGroupId,type,posIndex,robId,isWin,time,robInfo,actionType) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketReportData newMonopolyMarketReportData) {
        newMonopolyMarketReportData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketReportData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getResGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getPosIndex()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketReportData.getRobId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketReportData.getIsWin()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketReportData.getRobInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketReportData.getActionType()));
        if (getDb().executeNoneQuery(insertNewMonopolyMarketReportDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketReportData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketReportData newMonopolyMarketReportData = new NewMonopolyMarketReportData();
        newMonopolyMarketReportData.setUserId(rs.getLong("userId"));
        newMonopolyMarketReportData.setWeekId(rs.getInt("weekId"));
        newMonopolyMarketReportData.setResGroupId(rs.getInt("resGroupId"));
        newMonopolyMarketReportData.setType(rs.getInt("type"));
        newMonopolyMarketReportData.setPosIndex(rs.getInt("posIndex"));
        newMonopolyMarketReportData.setRobId(rs.getLong("robId"));
        newMonopolyMarketReportData.setIsWin(rs.getBoolean("isWin"));
        newMonopolyMarketReportData.setTime(rs.getInt("time"));
        newMonopolyMarketReportData.setRobInfo(JSON.parseObject(rs.getString("robInfo"), UserBaseInfo.class));

        newMonopolyMarketReportData.setActionType(rs.getInt("actionType"));
        return newMonopolyMarketReportData;
    }

}
